//
//  WDFirstSort.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/3/10.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDFirstSort.h"
#import "WDLinkMessage.h"
#import "WDCarList.h"

@implementation WDFirstSort

static NSString *compareStr = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

+ (NSArray *)firstSortWithObjectArray:(NSArray *)objArr
{
    NSMutableArray *array = [NSMutableArray array];
    
    int currentLoc = 0;
    int j = 0;
    NSDictionary *spacailDict;
    
    for (char i = 'A'; i <= 'Z'; i++) {
        
        NSMutableArray *tempArr = [NSMutableArray array];
        NSMutableArray *spacialArr = [NSMutableArray array];
        
        for (j = currentLoc; j < objArr.count; j++) {
            
            //改变当前的位置
            currentLoc = j;
            
            //判断首字母
            if ([[objArr[j] start_with] isEqualToString:[NSString stringWithFormat:@"%c", i]]) {
                
                [tempArr addObject:objArr[j]];
                
            } else {
                
                if ([compareStr rangeOfString:[objArr[j] start_with]].location ==NSNotFound) {//'#'分组
                    
                    [spacialArr addObject:objArr[j]];
                    
                } else {
                
                    break;
                    
                }
            }
        }
        
        //保存成字典
        if (spacialArr.count) {
            spacailDict = @{
                            @"firstLetter": @"#",
                            @"content": spacialArr
                            };
        }
        
        if (tempArr.count) {
            
            NSDictionary *dict = @{
                                   @"firstLetter": [objArr[j - 1] start_with],
                                   @"content": tempArr
                                   };
            
            [array addObject:dict];
        }
        
    }
    
    if (spacailDict.count) {
        
        [array addObject:spacailDict];
        
    }
    
    return array;
}

@end
